package com.yunlankeji.stemcells.activity.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.stemcells.model.request.UserInfo;
import com.yunlankeji.stemcells.network.HttpRequestUtil;
import com.yunlankeji.stemcells.network.NetWorkManager;
import com.yunlankeji.stemcells.network.callback.HttpRequestCallback;
import com.yunlankeji.stemcells.network.responsebean.ResponseBean;
import com.yunlankeji.stemcells.utils.LogUtil;
import com.yunlankeji.stemcells.utils.ToastUtil;

import retrofit2.Call;

import static com.yunlankeji.stemcells.network.NetWorkManager.getRequest;


public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";
    TextView textView1 ,textView2,textView3,textView4,textView5;
    TextView loginTextView,passwordTextView;

    private Context context = null;
    private PopupWindow popupWindow;
    private int from = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_login);


        textView5=(TextView)findViewById(R.id.textView13);

        textView1 = (TextView)findViewById(R.id.tv_login_login);
        loginTextView = (TextView)findViewById(R.id.editTextTextPersonName2);
        passwordTextView = (TextView)findViewById(R.id.editTextTextPassword2);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String loginName=loginTextView.getText().toString();
                String pwd=passwordTextView.getText().toString();
                if(TextUtils.isEmpty(loginName)){
                    ToastUtil.showShort("请输入用户名!");
                }else if(TextUtils.isEmpty(pwd)){
                    ToastUtil.showShort("请输入密码!");
                }else {
                    //发起接口调用
                    UserInfo userInfo=new UserInfo();
                    userInfo.setPhone(loginName);
                    userInfo.setPassword(pwd);
                    Call<ResponseBean> call = getRequest().requestLogin(userInfo);
                    //处理响应
                    HttpRequestUtil.httpRequest(call, new HttpRequestCallback() {
                        @Override
                        public void onSuccess(ResponseBean response) {
                            String data= JSONObject.toJSONString(response.data).toString();
                            UserInfo  user= JSON.parseObject(data,UserInfo.class);
                            LogUtil.d(TAG,"请求成功");
                            Intent intent=new Intent();
                            intent.setClass(LoginActivity.this,mine_about_usActivity.class);
                            startActivity(intent);
                        }

                        @Override
                        public void onFailure(String msg) {
                            LogUtil.d(TAG,"请求失败");
                        }

                        @Override
                        public void onDefeat(String code, String msg) {
                            ToastUtil.showShortForNet(msg);
                            LogUtil.d(TAG,"请求失败");

                        }
                    });
                }
            }
        });

        textView2=(TextView)findViewById(R.id.tv_login_register);
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

        textView3=(TextView)findViewById(R.id.tv_login_forgetpassword);
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,ForgetPasswordActivity.class);
                startActivity(intent);
            }
        });
        textView3=(TextView)findViewById(R.id.tv_login_user_agreement);
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,privacyActivity.class);
                startActivity(intent);
            }
        });
        textView4=(TextView)findViewById(R.id.tv_login_privacy_policy);
        textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,privacyActivity.class);
                startActivity(intent);
            }
        });



       /* textView5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch(v.getId()){
                    case R.id.textView13:{
                        from = Location.RIGHT.ordinal();
                        break;
                    }

                }

                initPopupWindow();

            }
        });*/
    }



/*

    public static class popupDismissListener implements PopupWindow.OnDismissListener{

        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }

    }
*/

/*


    protected void initPopupWindow(){
        View popupWindowView = getLayoutInflater().inflate(R.layout.activit_project_pop, null);
        //内容，高度，宽度
        if(Location.BOTTOM.ordinal() == from){
            popupWindow = new PopupWindow(popupWindowView, WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        }else{
            popupWindow = new PopupWindow(popupWindowView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.FILL_PARENT, true);
        }
        //动画效果
        if(Location.RIGHT.ordinal() == from){
            popupWindow = new PopupWindow(popupWindowView, 600, WindowManager.LayoutParams.FILL_PARENT, true);
            popupWindow.setAnimationStyle(R.style.AnimationRightFade);

        }

        //菜单背景色
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        popupWindow.setBackgroundDrawable(dw);
        //宽度
        //popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
        //高度
        //popupWindow.setHeight(LayoutParams.FILL_PARENT);
        //显示位置
        if(Location.RIGHT.ordinal() == from){
            popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.RIGHT, 0, 0);
        }
        //设置背景半透明
        backgroundAlpha(0.5f);
        //关闭事件
        popupWindow.setOnDismissListener(new popupDismissListener());

        popupWindowView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
				if( popupWindow!=null && popupWindow.isShowing()){
					popupWindow.dismiss();
					popupWindow=null;
				}
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                return false;
            }
        });


    }

    */
/**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     *//*

    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }
*/
/*    *
     * 菜单弹出方向
     **//*


    public enum Location {


        RIGHT,
        TOP,
        BOTTOM;

    }

*/


}